package com.example.service.impl;

import com.example.mapper.UserMapper;
import com.example.pojo.dto.UserInsertDTO;
import com.example.pojo.dto.UserPageQueryDTO;
import com.example.pojo.dto.UserUpdateDTO;
import com.example.pojo.entity.User;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.vo.UserPageVO;
import com.example.pojo.vo.UserVO;
import com.example.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    /**
     * 新增用户
     *
     * @param userInsertDTO
     */
    @Override
    public User insertUser(UserInsertDTO userInsertDTO) {
        //设置创建时间
        userInsertDTO.setCreateTime(LocalDateTime.now());
        userMapper.insertUser(userInsertDTO);
        return User.builder().id(userInsertDTO.getId()).build();
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @Override
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }

    /**
     * 修改用户信息
     *
     * @param userUpdateDTO
     */
    @Override
    public void updateUser(UserUpdateDTO userUpdateDTO) {
        userMapper.updateUser(userUpdateDTO);
    }

    /**
     * 查询用户信息
     *
     * @param id
     * @return
     */
    @Override
    public UserVO getUserById(Long id) {
        User user = userMapper.selectUserById(id);
        if (user == null) {
            return null;
        }
        return UserVO.builder()
                .id(user.getId())
                .surname(user.getSurname())
                .phone(user.getPhone())
                .sex(user.getSex())
                //将身份证号中间八位生日部分替换成星号
                //.idNumber(user.getIdNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"))
                .createTime(user.getCreateTime())
                .build();
    }

    /**
     * 分页查询用户信息
     *
     * @param userPageQueryDTO
     * @return
     */
    @Override
    public PageResult<UserPageVO> pageUser(UserPageQueryDTO userPageQueryDTO) {
        //设置分页条件
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());

        //执行查询
        List<User> userList = userMapper.listByQuery(userPageQueryDTO);

        List<UserPageVO> userPageVOList = new ArrayList<>();
        for (User user : userList) {
            UserPageVO userPageVO = new UserPageVO();
            BeanUtils.copyProperties(user, userPageVO);
            //将身份证号中间八位生日部分替换成星号
            userPageVO.setIdNumber(user.getIdNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
            userPageVOList.add(userPageVO);
        }

        Page<User> page = (Page<User>) userList;

        //返回分页结果
        return new PageResult<>(page.getTotal(), userPageVOList);
    }


}
